//index.js
//获取应用实例
var util = require('../../utils/util.js')
const app = getApp()
var _this = this
Page({
  data: {
    src_top: 'https://qushangstar.oss-cn-beijing.aliyuncs.com/resources/index_01.jpg',
    src_01: 'https://qushangstar.oss-cn-beijing.aliyuncs.com/resources/fenpei.jpg',
    src_02: 'https://qushangstar.oss-cn-beijing.aliyuncs.com/resources/xinnian.jpg',
    hiddenAuthorize: true,
    unAuthorized:false,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    num_01: 1231231,
    percent_01: 68,
    num_02: 1231231,
    percent_02: 74,
    src2: '',
    refereeId: 0,
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  enterMather :function() {
    var loginUserInfo = wx.getStorageSync('userInfo');//获取本地缓存中的loginDevice
    if (loginUserInfo == null || loginUserInfo == undefined){
      wx.showToast({
        title: '网络走神了，请稍候再试哦',
        icon: 'none'
      })
      return
    }
    let ticket = wx.getStorageSync('ticket')
    if (!ticket) {
      _this.setData({
        hiddenAuthorize: false,
        unAuthorized: true
      })
      // _this.handleLogin()
      // _this.authrizeRefresh()
      return
    }
    if (loginUserInfo.fillIn == 1){
        wx.navigateTo({
          // url: '../common/phoneAuthorize/phoneAuthorize'
          url: '../modules/peopleAssigned/matchResult/matchResult'
        })
    } else if (loginUserInfo.fillIn !== 1) {
        wx.navigateTo({
        // url: '../common/phoneAuthorize/phoneAuthorize'
        url: '../modules/peopleAssigned/infoGather/infoGather'
        })
     }
  },
  onLoad: function (options) {
    _this = this
    _this.setData({
      hiddenAuthorize: true
    })
    if (options.id != null && options.id != undefined){
      _this.data.refereeId = options.id
    }
    _this.authrizeRefresh()
    // util.getSession(_this.didGetSession)
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
        unAuthorized: false
      })
      _this.handleLogin()
      _this.authrizeRefresh()
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          unAuthorized: false
        })
        _this.handleLogin()
        _this.authrizeRefresh()
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
            unAuthorized: false
          })
          _this.authrizeRefresh()
          _this.handleLogin()
        }
        
      })
    }
  },
  didGetData: (data) => {
    console.log(data)
    _this.setData({
      src2: data
    })
  },
  didGetSession: (access_token) => {
    console.log("access_token key:"+access_token)
    util.getQrode(_this.didGetData, access_token)
  },
  authorizeFinished: (e) => {
    _this.setData({
      unAuthorized: app.globalData.unAuthorized
    })
    console.log(app.globalData.userInfo)
    _this.handleLogin()
    _this.authrizeRefresh()
  },
  authrizeRefresh: () => {
    if (app.globalData.userInfo) {
      _this.setData({
        unAuthorized: false
      })
      wx.setNavigationBarTitle({
        title: '脉洞',
      })
    } else {
      _this.setData({
        hiddenAuthorize: false,
        unAuthorized: true
      })
      wx.setNavigationBarTitle({
        title: '手机授权',
      })
    }
  },
  handleLogin: () => {
    util.login(_this.loginDidResponse, _this.loginDidFailed,_this.data.refereeId)
  },
  loginDidResponse: (res) => {
      console.log('did receive response: '+res)
      if (res.status == '200'){
        wx.setStorageSync('ticket', res.data.ticket)
        wx.setStorageSync('userInfo', res.data.userInfo)
        var ticket = wx.getStorageSync('ticket'); //获取本地缓存中的userIdEnc //用户唯一识别码
        var loginUserInfo = wx.getStorageSync('userInfo');//获取本地缓存中的loginDevice
        console.log(ticket)
        console.log(loginUserInfo)
      }
  },
  loginDidFailed: (res) => {

  }
})
